//
// Created by yangchao on 2022/5/10.
// 56. 合并区间: https://leetcode.cn/problems/merge-intervals/
//
#include <vector>
#include <algorithm>
using namespace std;

class Merge{
public:
    /*怎么确定是左排序还是右排序呢？？？将列表中的区间按照左端点升序排序 */
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        if (intervals.size() == 0) return {};
        //sort(intervals.begin(),intervals.end(),[](vector<int>& v1, vector<int>& v2){return v1[0] - v2[0];});
        sort(intervals.begin(),intervals.end());
        vector<vector<int>> res;
        res.push_back(intervals[0]);
        for (int i = 1; i < intervals.size(); ++i) {
            if (res.back()[1] < intervals[i][0]) {
                res.push_back(intervals[i]);
            } else {
                res.back()[1] = max(res.back()[1],intervals[i][1]);
            }
        }
        return res;
    }
};

